<?php

/**
 * note actions.
 *
 * @package		gesp
 * @subpackage note
 * @author		 Your name here
 * @version		SVN: $Id: actions.class.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class noteActions extends sfActions {
	public function executeIndex(sfWebRequest $request) {
		$prj_id = $this->getUser()->getAttribute('prj_id');
		$this->forward404Unless(Doctrine_Core::getTable('Projet')->find($prj_id), sprintf('Object tache does not exist (%s).', $prj_id));
		$q = Doctrine_Query::create()
			->from('Note')
			->andwhere('nte_prj_id = ?', $prj_id)
			->orderBy('nte_date asc');
		$this->note_list = $q->execute();
	}
	
	
	//-------------------------------------------------------------------------------
	// nouvelle note projet
	//-------------------------------------------------------------------------------
	public function executeNew(sfWebRequest $request) {
		$prj_id = $this->getUser()->getAttribute('prj_id');
		$this->forward404Unless(Doctrine_Core::getTable('Projet')->find($prj_id), sprintf('Object tache does not exist (%s).', $prj_id));
		$note = new Note();
		$note->setNtePrjId($prj_id);
		$this->form = new NoteForm($note);
	}

	public function executeCreate(sfWebRequest $request) {
		$this->forward404Unless($request->isMethod(sfRequest::POST));

		$this->form = new NoteForm();

		$this->processForm($request, $this->form);

		$this->setTemplate('new');
	}

	//-------------------------------------------------------------------------------
	// nouvelle note tache
	//-------------------------------------------------------------------------------
	public function executeNew_tache(sfWebRequest $request) {
		$tac_id = $request->getParameter('tac_id');
		$this->forward404Unless(Doctrine_Core::getTable('Tache')->find($tac_id), sprintf('Object tache does not exist (%s).', $tac_id));
		$note = new Tache_Note();
		$note->setTacnteTacId($tac_id);
		$this->form = new Tache_NoteForm($note);
	}

	public function executeCreate_tache(sfWebRequest $request) {
		$this->forward404Unless($request->isMethod(sfRequest::POST));

		$this->form = new Tache_NoteForm();

		$this->processForm_tache($request, $this->form);

		$this->setTemplate('new_tache');
	}

	//-------------------------------------------------------------------------------
	// modification note projet
	//-------------------------------------------------------------------------------
	public function executeEdit(sfWebRequest $request) {
		$this->forward404Unless($note = Doctrine_Core::getTable('Note')->find(array($request->getParameter('nte_id'))), sprintf('Object note does not exist (%s).', $request->getParameter('nte_id')));
		$this->form = new NoteForm($note);
	}

	public function executeUpdate(sfWebRequest $request) {
		$this->forward404Unless($request->isMethod(sfRequest::POST) || $request->isMethod(sfRequest::PUT));
		$this->forward404Unless($note = Doctrine_Core::getTable('Note')->find(array($request->getParameter('nte_id'))), sprintf('Object note does not exist (%s).', $request->getParameter('nte_id')));
		$this->form = new NoteForm($note);

		$this->processForm($request, $this->form);

		$this->setTemplate('edit');
	}

	//-------------------------------------------------------------------------------
	// modification note tache
	//-------------------------------------------------------------------------------
	public function executeEdit_tache(sfWebRequest $request) {
		$this->forward404Unless($note = Doctrine_Core::getTable('Tache_Note')->find(array($request->getParameter('nte_id'))), sprintf('Object note does not exist (%s).', $request->getParameter('nte_id')));
		$this->form = new Tache_NoteForm($note);
	}

	public function executeUpdate_tache(sfWebRequest $request) {
		$this->forward404Unless($request->isMethod(sfRequest::POST) || $request->isMethod(sfRequest::PUT));
		$this->forward404Unless($note = Doctrine_Core::getTable('Tache_Note')->find(array($request->getParameter('nte_id'))), sprintf('Object note does not exist (%s).', $request->getParameter('nte_id')));
		$this->form = new Tache_NoteForm($note);

		$this->processForm_tache($request, $this->form);

		$this->setTemplate('edit_tache');
	}

	//-------------------------------------------------------------------------------
	// suppression note projet
	//-------------------------------------------------------------------------------
	public function executeDelete(sfWebRequest $request) {
		$request->checkCSRFProtection();

		$this->forward404Unless($note = Doctrine_Core::getTable('Note')->find(array($request->getParameter('nte_id'))), sprintf('Object note does not exist (%s).', $request->getParameter('nte_id')));
		$note->delete();
		$this->redirect('note/index');
	}

	//-------------------------------------------------------------------------------
	// suppression note tache
	//-------------------------------------------------------------------------------
	public function executeDelete_tache(sfWebRequest $request) {
		$request->checkCSRFProtection();

		$this->forward404Unless($note = Doctrine_Core::getTable('Tache_Note')->find(array($request->getParameter('nte_id'))), sprintf('Object note does not exist (%s).', $request->getParameter('nte_id')));
		$tache_id = $note->getTacnteTacId();
		$note->delete();

		$this->redirect('tache/show?tac_id='.$tache_id);
	}

	
	
	
	//-------------------------------------------------------------------------------
	// process form projet
	//-------------------------------------------------------------------------------
	protected function processForm(sfWebRequest $request, sfForm $form) {
		$form->bind($request->getParameter($form->getName()), $request->getFiles($form->getName()));
		if ($form->isValid()) {
			$note = $form->save();
			$this->redirect('note/index');
		}
	}

	//-------------------------------------------------------------------------------
	// process form tache
	//-------------------------------------------------------------------------------
	protected function processForm_tache(sfWebRequest $request, sfForm $form) {
		$form->bind($request->getParameter($form->getName()), $request->getFiles($form->getName()));
		if ($form->isValid()) {
			$note = $form->save();
			$this->redirect('tache/show?tac_id='.$note->getTacnteTacId());
		}
	}
}
